<template>
  <div class="dashboard-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <h2>学生实验进度监控</h2>
          <div class="header-actions">
            <el-select 
              v-model="selectedExperiment" 
              placeholder="筛选实验"
              clearable
              @change="fetchProgress"
            >
              <el-option
                v-for="exp in experimentOptions"
                :key="exp.id"
                :label="exp.name"
                :value="exp.id"
              />
            </el-select>
            <el-button 
              type="primary" 
              :icon="Refresh" 
              @click="fetchProgress"
            >
              刷新数据
            </el-button>
          </div>
        </div>
      </template>

      <el-table 
        :data="progressList" 
        v-loading="loading"
        stripe
        border
        style="width: 100%"
        max-height="500px"
      >
        <el-table-column 
          prop="student_name" 
          label="学生姓名" 
          width="120"
          align="center"
        />
        <el-table-column 
          prop="experiment_name" 
          label="实验名称" 
          min-width="180"
        />
        <el-table-column 
          prop="status" 
          label="完成状态" 
          width="120"
          align="center"
        >
          <template #default="{ row }">
            <el-tag 
              :type="row.status === '已完成' ? 'success' : 'warning'"
              effect="dark"
            >
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column 
          prop="completed_at" 
          label="完成时间" 
          width="180"
          align="center"
        />
        <el-table-column 
          prop="score" 
          label="得分" 
          width="100"
          align="center"
        >
          <template #default="{ row }">
            <el-tag v-if="row.score !== null" type="info">
              {{ row.score }} / {{ row.max_score || '--' }}
            </el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column 
          label="操作" 
          width="120"
          align="center"
        >
          <template #default="{ row }">
            <el-button 
              type="primary" 
              size="small" 
              @click="viewDetail(row)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="fetchProgress"
          @current-change="fetchProgress"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { Refresh } from '@element-plus/icons-vue'

const progressList = ref([])
const loading = ref(false)
const selectedExperiment = ref('')
const experimentOptions = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const fetchExperiments = async () => {
  try {
    const res = await axios.get('/api/teacher/experiments/')
    experimentOptions.value = res.data
  } catch (error) {
    console.error('获取实验列表失败:', error)
  }
}

const fetchProgress = async () => {
  try {
    loading.value = true
    const params = {
      page: currentPage.value,
      page_size: pageSize.value,
      experiment: selectedExperiment.value || undefined
    }
    const res = await axios.get('/api/student-progress/', { params })
    progressList.value = res.data.results || res.data
    total.value = res.data.count || res.data.length
  } catch (error) {
    console.error('获取进度失败:', error)
  } finally {
    loading.value = false
  }
}

const viewDetail = (row) => {
  // 这里可以跳转到详情页或打开对话框
  console.log('查看详情:', row)
}

onMounted(() => {
  fetchExperiments()
  fetchProgress()
})
</script>

<style scoped>
.dashboard-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
  max-width: 1400px;
  margin: 0 auto;
}

.box-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  width: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-table {
  margin-top: 15px;
}

.el-tag {
  font-weight: bold;
}
</style>